##########################################################################################
#To make this figure, you must first run "scaling_ideal_points_all_methods.R" first.######
##########################################################################################

setwd("~/Desktop/QJPS Replication")
rm(list = ls())
library(foreign)

data <- read.csv("all_countries_ideal.csv")
data <- data[order(data$median),]

maxyear <- tapply(X = data$year, data$cown, max, na.rm = T)
m <- match(data$cown, names(maxyear))
table(is.na(m))
data$maxyear <- maxyear[m]
data <- data[data$year == data$maxyear,]

names <- as.character(data$name)
names[names == "Congo, Democratic Republic Of (Zaire)"] <- "D.R. Congo"
names[names == "Belarus (Byelorussia)"] <- "Belarus"
names[names == "Burkina Faso (Upper Volta)"] <- "Burkina Faso"
names[names == "Cambodia (Kampuchea)"] <- "Cambodia"
names[names == "Iran (Persia)"] <- "Iran"
names[names == "Macedonia (Former Yugoslav Republic Of)"] <- "Macedonia"
names[names == "Micronesia, Fed. Sts."] <- "Micronesia"
names[names == "Myanmar (Burma)"] <- "Myanmar"
names[names == "Russia (Soviet Union)"] <- "Russia"
names[names == "Sri Lanka (Ceylon)"] <- "Sri Lanka"
names[names == "Tanzania/Tanganyika"] <- "Tanzania"
names[names == "Turkey/Ottoman Empire"] <- "Turkey"
names[names == "Yemen (Arab Republic Of Yemen)"] <- "Yemen"
names[names == "Yemen, People'S Republic Of"] <- "Yemen"
names[names == "Yugoslavia (Serbia)"] <- "Yugoslavia"
names[names == "Zimbabwe (Rhodesia)"] <- "Zimbabwe"
names[names == "Korea, People'S Republic Of"] <- "N. Korea"
names[names == "Korea, Republic Of"] <- "S. Korea"
names[names == "Madagascar (Malagasy)"] <- "Madagascar"
names[names == "Kyrgyz Republic"] <- "Kyrgyzstan"
names[names == "Vietnam, Democratic Republic Of"] <- "N. Vietnam"
names[names == "Vietnam, Republic Of"] <- "S. Vietnam"

#####################################
#First Dimension Catepiller plot
#####################################par(mar = c(5, 3, 3, 3))
plot(1:nrow(data), data$median, pch = 16, cex = .5, axes = F, xlab="", ylab = "", ylim = c(-2.7, 2.2))
axis(1, at = seq(1:(nrow(data))), labels = names, cex.axis = .3, las = 2)
axis(2, at = round(seq(-3, 3, .5), 1), cex.axis = 1, las = 2)
mtext("Constitution Similarity Score - Contemporary States", side = 3, at = 90, cex = 1.5, line = .5)
segments(x0 = 1:nrow(data), x1 = 1:nrow(data), y0 = data$lower, y1 = data$upper)
box()